package com.sen.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sen.dao.EmpDao;
import com.sen.dao.EmpRoleDao;
import com.sen.entity.DruginfoEntity;
import com.sen.entity.EmpEntity;
import com.sen.entity.EmpRoleEntity;
import com.sen.service.EmpService;
import com.sen.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

@Service("empService")
public class EmpServiceImpl extends ServiceImpl<EmpDao, EmpEntity> implements EmpService {

    @Autowired
    private EmpDao empDao;
    @Autowired
    private EmpRoleDao empRoleDao;
    @Override
    public IPage<EmpEntity> queryAll(Page<EmpEntity> page, Map<String, String> emp) {
        QueryWrapper<EmpEntity> wrapper = new QueryWrapper<>();
        if (emp.get("name")!=null){
            wrapper.like("name", emp.get("name"));
        }
        if (emp.get("phone")!=null){
            wrapper.like("phone", emp.get("phone"));
        }
        if (emp.get("createDate")!=null){
            wrapper.between("createDate", emp.get("createDate[0]"),emp.get("createDate[1]"));
        }
        return empDao.queryAll(page,wrapper);
    }

    @Override
    @Transactional
    public void addEmp(EmpEntity empEntity, Integer roleId) {
        //设置用户初始默认密码123456
        empEntity.setPassword(new BCryptPasswordEncoder().encode("123456"));
        //添加新员工
        empDao.insert(empEntity);
        EmpRoleEntity empRole = new EmpRoleEntity();
        empRole.setEid(empEntity.getEid());
        empRole.setRoleid(roleId);
        //添加员工角色关联表
        empRoleDao.insert(empRole);
    }
}
